package com.bw.lib_log.logger

import com.bw.lib_log.LoggerLevel
import com.bw.lib_log.LoggerStrategy

class Logger constructor(
    private val TAG: String,
    private val level: LoggerLevel,
    private val strategy: LoggerStrategy,
    private val saveUrl: String,
    private val isDebug: Boolean
) {
    //次构造继承主构造
    constructor(builder: Builder):this(
        builder.TAG,
        builder.level,
        builder.strategy,
        builder.saveUrl,
        builder.isDebug
    )
    /**
     * 策略者模式 根据不同的策略创建不同的logger对象
     */
    private var logger: ILogger//日志logger

    init {
        when(strategy){
            LoggerStrategy.LOG_CAT -> logger = LogCatLogger()
            LoggerStrategy.FILE -> logger = FileLogger()
            LoggerStrategy.NET -> logger = NetLogger()
        }
        logger.setDebug(isDebug)
        logger.setLevel(level)
        logger.setTag(TAG)
        logger.setSavaUrl(saveUrl)
    }
    fun d(tag:String,log:String){
        logger.d(tag,log)
    }
    fun i(tag:String,log:String){
        logger.i(tag,log)
    }
    fun v(tag:String,log:String){
        logger.v(tag,log)
    }
    fun w(tag:String,log:String){
        logger.w(tag,log)
    }
    fun e(tag:String,log:String){
        logger.e(tag,log)
    }

    /**
     * 构建者设计模式 多个参数的按需设置加载
     */
    class Builder {
        var isDebug:Boolean = true
        var TAG = "默认TAG"
        var level:LoggerLevel = LoggerLevel.DEBUG
        var strategy:LoggerStrategy = LoggerStrategy.LOG_CAT
        var saveUrl:String = "默认路径"

        fun setTAG(TAG:String):Builder{
            this.TAG = TAG
            return this
        }
        fun setDebug(isDebug:Boolean):Builder{
            this.isDebug = isDebug
            return this
        }
        fun setLevel(level:LoggerLevel):Builder{
            this.level = level
            return this
        }
        fun setStrategy(strategy:LoggerStrategy):Builder{
            this.strategy = strategy
            return this
        }
        fun setSaveUrl(saveUrl:String):Builder{
            this.saveUrl = saveUrl
            return this
        }
        fun build():Logger{
            return Logger(this)
        }
    }
}